package com.deying.actionbarcompat;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ActionBarSpinner {
	SimpleAdapter adapter;
	OnPopItemClickListener onPopItemClickListener = null;
	PopupWindow popupWindow;
	Button downView;
	private boolean isWrapContent;

	public static ActionBarSpinner getPopWindowInstance(Context paramContext,
			List<HashMap<String, String>> paramList, Button paramDownView) {
		int width = (int) (paramContext.getResources().getDisplayMetrics().widthPixels / 3);
		int height = (int) (paramContext.getResources().getDisplayMetrics().heightPixels / 1.694);
		return new ActionBarSpinner(paramContext, paramList, paramDownView, width,
				height);
	}

	public static ActionBarSpinner getPopWindowInstanceHeightWrap(
			Context paramContext, List<HashMap<String, String>> paramList,
			Button paramDownView) {
		int width = (int) (paramContext.getResources().getDisplayMetrics().widthPixels / 3);
		int height = ViewGroup.LayoutParams.WRAP_CONTENT;
		return new ActionBarSpinner(paramContext, paramList, paramDownView, width,
				height);
	}

	private ActionBarSpinner(Context paramContext,
			List<HashMap<String, String>> paramList, Button paramDownView,
			int width, int height) {
		this.downView = paramDownView;
		View fliter_fragment = LayoutInflater.from(paramContext).inflate(
				R.layout.spinner_fliter_fragment, null);
		popupWindow = new PopupWindow(fliter_fragment, width, height, true);
		ListView lvCategory = (ListView) fliter_fragment
				.findViewById(R.id.lv_category);
		String[] from = { "id", "name" };
		int[] to = { R.id.category_detail_key, R.id.category_detail_textVeiw };
		adapter = new SimpleAdapter(paramContext, paramList,
				R.layout.spinner_filter_sub_list_item, from, to);
		lvCategory.setAdapter(adapter);
		lvCategory.setOnItemClickListener(new ListView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				LinearLayout valueLayout = (LinearLayout) view;
				TextView mTextView = (TextView) valueLayout
						.findViewById(R.id.category_detail_textVeiw);
				downView.setText(mTextView.getText().toString());
				// String mKey = ((TextView)
				// valueLayout.findViewById(R.id.category_detail_key)).toString();
				popupWindow.dismiss();
				if (onPopItemClickListener != null) {
					onPopItemClickListener
							.loadData((HashMap<String, String>) adapter
									.getItem(position));
				}
			}
		});
	}

	public void show() {
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.update();
		popupWindow.showAsDropDown(downView);
	}

	public void setOnPopItemClickListener(OnPopItemClickListener listener) {
		onPopItemClickListener = listener;
	}

	public interface OnPopItemClickListener {
		void loadData(HashMap<String, String> map);
	}
}
